from django.shortcuts import render
from my_bgyp.models import *
from django.http import JsonResponse
import json
from django.db.models import Q
def get_kucun(request):
    # 获取所有库存信息
    try:
        obj_kucun = kucun.objects.all().values()
        #转换为标准list
        kucundata = list(obj_kucun)
        return JsonResponse({'code':1,'data':kucundata})
    except Exception as e:
        return JsonResponse({'code':0, 'msg': '获取学生信息出现异常，具体错误'+str(e)})

def search_kucun(request):
    #接收查询关键字
    data = json.loads(request.body.decode('utf-8'))
    #获取搜索库存信息
    try:
        sea_kucun = kucun.objects.filter(Q(name__icontains=data['inputstr'])|Q(kC_choice__icontains=data['inputstr'])|Q(num__icontains=data['inputstr'])|Q(price__icontains=data['inputstr'])|Q(time__icontains=data['inputstr'])|Q(person__icontains=data['inputstr'])|Q(remarks__icontains=data['inputstr'])).values()
        kucundata = list(sea_kucun)
        # print(kucundata)
        return JsonResponse({'code':1,'data':kucundata})
    except Exception as e:
        print(e)
        return JsonResponse({'code':0,'msg':'获取学生信息出现异常，具体错误'+str(e)})